<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vision Pro 摄像头权限演示</title>
    <style>
        body {
            margin: 0;
            padding: 20px;
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            color: white;
        }
        
        .container {
            max-width: 800px;
            margin: 0 auto;
            background: rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(20px);
            border-radius: 20px;
            padding: 40px;
            border: 1px solid rgba(255, 255, 255, 0.2);
        }
        
        h1 {
            text-align: center;
            margin-bottom: 30px;
            font-size: 2.5em;
            font-weight: 700;
            text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
        }
        
        .status-card {
            background: rgba(255, 255, 255, 0.15);
            border-radius: 15px;
            padding: 20px;
            margin: 20px 0;
            border: 1px solid rgba(255, 255, 255, 0.2);
        }
        
        .button {
            background: rgba(255, 255, 255, 0.2);
            border: 2px solid rgba(255, 255, 255, 0.3);
            color: white;
            padding: 15px 30px;
            border-radius: 12px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            margin: 10px;
            transition: all 0.3s ease;
            backdrop-filter: blur(10px);
        }
        
        .button:hover {
            background: rgba(255, 255, 255, 0.3);
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(0, 0, 0, 0.2);
        }
        
        .button.primary {
            background: #007AFF;
            border-color: #007AFF;
        }
        
        .button.primary:hover {
            background: #0056CC;
            border-color: #0056CC;
        }
        
        .button.success {
            background: #34C759;
            border-color: #34C759;
        }
        
        .button.danger {
            background: #FF3B30;
            border-color: #FF3B30;
        }
        
        .status-indicator {
            display: inline-block;
            width: 12px;
            height: 12px;
            border-radius: 50%;
            margin-right: 8px;
        }
        
        .status-granted { background: #34C759; }
        .status-denied { background: #FF3B30; }
        .status-prompt { background: #FF9500; }
        .status-unknown { background: #8E8E93; }
        
        .log-container {
            background: rgba(0, 0, 0, 0.3);
            border-radius: 10px;
            padding: 20px;
            margin: 20px 0;
            max-height: 300px;
            overflow-y: auto;
            font-family: 'Monaco', 'Menlo', monospace;
            font-size: 14px;
            line-height: 1.4;
        }
        
        .log-entry {
            margin: 5px 0;
            padding: 5px 0;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        }
        
        .log-entry:last-child {
            border-bottom: none;
        }
        
        .device-info {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 15px;
            margin: 20px 0;
        }
        
        .info-item {
            background: rgba(255, 255, 255, 0.1);
            padding: 15px;
            border-radius: 10px;
            text-align: center;
        }
        
        .info-label {
            font-size: 12px;
            opacity: 0.8;
            margin-bottom: 5px;
        }
        
        .info-value {
            font-size: 16px;
            font-weight: 600;
        }
        
        @media (max-width: 768px) {
            .container {
                margin: 10px;
                padding: 20px;
            }
            
            h1 {
                font-size: 2em;
            }
            
            .button {
                width: 100%;
                margin: 5px 0;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>📷 Vision Pro 摄像头权限演示</h1>
        
        <div class="device-info">
            <div class="info-item">
                <div class="info-label">设备类型</div>
                <div class="info-value" id="device-type">检测中...</div>
            </div>
            <div class="info-item">
                <div class="info-label">浏览器</div>
                <div class="info-value" id="browser-info">检测中...</div>
            </div>
            <div class="info-item">
                <div class="info-label">WebXR 支持</div>
                <div class="info-value" id="webxr-support">检测中...</div>
            </div>
            <div class="info-item">
                <div class="info-label">安全上下文</div>
                <div class="info-value" id="secure-context">检测中...</div>
            </div>
        </div>
        
        <div class="status-card">
            <h3>📋 权限状态</h3>
            <p>
                <span class="status-indicator" id="permission-indicator"></span>
                <span id="permission-status">检查中...</span>
            </p>
            <div style="margin-top: 15px;">
                <button class="button primary" onclick="checkPermission()">检查权限状态</button>
                <button class="button primary" onclick="requestPermission()">请求摄像头权限</button>
                <button class="button" onclick="showPermissionGuide()">显示权限指南</button>
                <button class="button danger" onclick="clearLogs()">清空日志</button>
            </div>
        </div>
        
        <div class="status-card">
            <h3>📝 操作日志</h3>
            <div class="log-container" id="log-container">
                <div class="log-entry">等待操作...</div>
            </div>
        </div>
    </div>

    <script type="module">
        import { CameraPermissionManager } from './js/utils/CameraPermissionManager.js';
        
        let permissionManager;
        
        // 初始化
        async function init() {
            permissionManager = new CameraPermissionManager();
            
            // 检测设备信息
            updateDeviceInfo();
            
            // 检查初始权限状态
            await checkPermission();
            
            // 添加权限状态监听器
            permissionManager.addPermissionListener((status) => {
                updatePermissionStatus(status);
                addLog(`权限状态变化: ${status}`, 'info');
            });
            
            addLog('摄像头权限管理器初始化完成', 'success');
        }
        
        // 更新设备信息
        function updateDeviceInfo() {
            const userAgent = navigator.userAgent;
            const isVisionPro = permissionManager.detectVisionPro();
            
            document.getElementById('device-type').textContent = isVisionPro ? 'Vision Pro' : '其他设备';
            document.getElementById('browser-info').textContent = getBrowserInfo();
            document.getElementById('webxr-support').textContent = 'xr' in navigator ? '✅ 支持' : '❌ 不支持';
            document.getElementById('secure-context').textContent = window.isSecureContext ? '✅ 安全' : '❌ 不安全';
        }
        
        // 获取浏览器信息
        function getBrowserInfo() {
            const userAgent = navigator.userAgent.toLowerCase();
            if (userAgent.includes('safari') && !userAgent.includes('chrome')) {
                return 'Safari';
            } else if (userAgent.includes('chrome')) {
                return 'Chrome';
            } else if (userAgent.includes('firefox')) {
                return 'Firefox';
            } else if (userAgent.includes('edge')) {
                return 'Edge';
            }
            return '未知';
        }
        
        // 检查权限状态
        window.checkPermission = async function() {
            addLog('开始检查摄像头权限状态...', 'info');
            
            try {
                const status = await permissionManager.checkPermissionStatus();
                updatePermissionStatus(status);
                addLog(`权限状态: ${status}`, 'success');
            } catch (error) {
                addLog(`权限检查失败: ${error.message}`, 'error');
            }
        };
        
        // 请求权限
        window.requestPermission = async function() {
            addLog('开始请求摄像头权限...', 'info');
            
            try {
                const result = await permissionManager.requestCameraPermission();
                
                if (result.success) {
                    addLog(`权限请求成功: ${result.message}`, 'success');
                    updatePermissionStatus('granted');
                } else {
                    addLog(`权限请求失败: ${result.message}`, 'error');
                    updatePermissionStatus(result.status);
                }
            } catch (error) {
                addLog(`权限请求异常: ${error.message}`, 'error');
            }
        };
        
        // 显示权限指南
        window.showPermissionGuide = function() {
            addLog('显示权限设置指南', 'info');
            permissionManager.showPermissionGuide();
        };
        
        // 更新权限状态显示
        function updatePermissionStatus(status) {
            const indicator = document.getElementById('permission-indicator');
            const statusText = document.getElementById('permission-status');
            
            // 移除所有状态类
            indicator.className = 'status-indicator';
            
            switch (status) {
                case 'granted':
                    indicator.classList.add('status-granted');
                    statusText.textContent = '✅ 已授予摄像头权限';
                    break;
                case 'denied':
                    indicator.classList.add('status-denied');
                    statusText.textContent = '❌ 摄像头权限被拒绝';
                    break;
                case 'prompt':
                    indicator.classList.add('status-prompt');
                    statusText.textContent = '⏳ 等待用户授权';
                    break;
                case 'unavailable':
                    indicator.classList.add('status-unknown');
                    statusText.textContent = '📷 未找到摄像头设备';
                    break;
                case 'security_error':
                    indicator.classList.add('status-denied');
                    statusText.textContent = '🔒 安全错误：需要HTTPS';
                    break;
                default:
                    indicator.classList.add('status-unknown');
                    statusText.textContent = '❓ 权限状态未知';
            }
        }
        
        // 添加日志
        function addLog(message, type = 'info') {
            const logContainer = document.getElementById('log-container');
            const logEntry = document.createElement('div');
            logEntry.className = 'log-entry';
            
            const timestamp = new Date().toLocaleTimeString();
            const icon = type === 'success' ? '✅' : type === 'error' ? '❌' : type === 'warning' ? '⚠️' : 'ℹ️';
            
            logEntry.innerHTML = `<span style="opacity: 0.7;">[${timestamp}]</span> ${icon} ${message}`;
            
            logContainer.appendChild(logEntry);
            logContainer.scrollTop = logContainer.scrollHeight;
        }
        
        // 清空日志
        window.clearLogs = function() {
            const logContainer = document.getElementById('log-container');
            logContainer.innerHTML = '<div class="log-entry">日志已清空</div>';
        };
        
        // 页面加载完成后初始化
        document.addEventListener('DOMContentLoaded', init);
        
        // 页面卸载时清理
        window.addEventListener('beforeunload', () => {
            if (permissionManager) {
                permissionManager.dispose();
            }
        });
    </script>
</body>
</html>